//
//  first_opengl_program.cpp
//  FirstOpenGLProject
//
//  Created by alan ren on 2019/9/10.
//  Copyright © 2019 alan ren. All rights reserved.
//

#define GL_SILENCE_DEPRECATION

#include<GLUT/GLUT.h>
#include<math.h>

using namespace std;

#define  PI  3.1415926


int showWindowWidth = 400;
int showWindowhHeight = 300;
int windowLcationLeft = 0;
int windowLcationTop = 0;


void drawPlane(){
    glColor3f(0.6f, 0.6f, 0.6f);
    
    glBegin(GL_LINES);
    for (float u = -1.0f;u<1.01f;u+=0.2f){
        glVertex3d(-1, 0, u);
        glVertex3d(1, 0, u);
        
        glVertex3d(u, 0, -1);
        glVertex3d(u, 0, 1);
    }
    glEnd();
}

void onDisPaly(){
    //rgba
    glClearColor(1,1,1,1);
    //清除缓存
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    double m[16] ={0};
    
    double theta = 20 *PI/180;
    double alpha = 30 *PI/180;
    double beta = 45 *PI/180;
    
    //平移图像
    double translateX[16] ={0};
    //变换都是用单位矩阵，所以主对角线都是1
    translateX[0] = translateX[5]= translateX[10]=translateX[15] = 1;
    translateX[14] =-0.5;
    glMultMatrixd(translateX);
    
    //旋转图像
    m[0] = m[5]= m[10]=m[15] = 1;
    m[0]= cos(alpha);
    m[1]= sin(alpha)*sin(theta);
    m[2]=-sin(alpha)*cos(beta);
    m[5]= cos(beta);
    m[6]= sin(beta);
    m[8]=sin(alpha);
    m[9]= -cos(alpha)*sin(beta);
    m[10] =cos(alpha)*cos(beta);
    
    glMultMatrixd(m);
    
    
    
    drawPlane();
    
    glPushMatrix();
    glTranslatef(-0.3f,0,-0.3f);
    glColor3f(1,0, 0);
    glutWireCube(0.4f);
    glPopMatrix();
    
    glPushMatrix();
    glTranslatef(0.3f,0,-0.3f);
    glColor3f(0,1, 0);
    glutWireSphere(0.2f, 16, 16);
    glPopMatrix();
    
    glPushMatrix();
    glRotated(-90, 1, 0, 0);
    glColor3f(1,1, 0);
    glutWireCone(0.2f, 0.6f, 16, 8);
    glPopMatrix();
    
    glEnd();
    //交换缓存
    glutSwapBuffers();
}



void onReShap(int w,int h){
    glViewport(0,0,w,h);
    double aspect = (double) w/(double)h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(aspect>=1){
        glOrtho(-aspect,aspect,-1,1,0,1);
    }else if(aspect<1){
        glOrtho(-1/aspect,1/aspect,-1,1,0,1);
    }
    glMatrixMode(GL_MODELVIEW);
    glFlush();
}

int getScreenWidth(){
    return glutGet(GLUT_SCREEN_WIDTH);
}


int getScreenHeight(){
    return glutGet(GLUT_SCREEN_HEIGHT);
}

void setWindowCreenWidthAndhHeight(int width ,int height){
    if (width!=0&&height!=0) {
        showWindowWidth = width;
        showWindowhHeight = height;
    }
}

void setWindowIsCenter(bool isCenter){
    if(!isCenter){
        return;
    }
    windowLcationLeft = getScreenWidth()/2-showWindowWidth/2;
    windowLcationTop = getScreenHeight()/2-showWindowhHeight/2;
    glutInitWindowPosition(windowLcationLeft,windowLcationTop);
}

void setWindowLocation(int left,int top){
    windowLcationTop = top;
    windowLcationLeft = left;
    glutInitWindowPosition(windowLcationLeft,windowLcationTop);
}

int main(int argc, char *argv[])
{
    //初始化glut
    glutInit(&argc ,argv);
    //设置OPengl的显示模式（双缓存，RGB颜色模式，深度缓存）
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    //设置窗口初始尺寸
    glutInitWindowSize(showWindowWidth,showWindowhHeight);
    //设置窗口初始位置
    setWindowIsCenter(true);
    //设置窗口标题
    glutCreateWindow("画了个变换图形！");
    //设置显示问题
    glutDisplayFunc(onDisPaly);
    
    //onReShap回调
    glutReshapeFunc(onReShap);
    
    glutMainLoop();
    
    return 0;
}
